/*
============================================================================
FileName	:	XQChatRosterScene.cpp
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-17 13:47
Change Data	:	2012-03-17 13:47 by liyake
Detail		:	The socket client of xqchat application
============================================================================
*/

#include "XQChatRosterScene.h"
#include "XQChatClientManager.h"
#include "XQChatTalkGroup.h"

#include <QtGui>
using namespace XQChat;

XQChat::CXQChatRosterScene::CXQChatRosterScene( QObject * parent /*= 0*/ )
	: QGraphicsScene(parent)
{
	m_pGroup = addRosterGroupIfDontExist("All");

	/*testing an talk widget in the scene.*/
	addItem(CXQChatTalkGroup::getXQChatTalkGroup());
	CXQChatTalkGroup::getXQChatTalkGroup()->setPos(50,50);
	/************************************************************************/
}

XQChat::CXQChatRosterScene::~CXQChatRosterScene( void )
{

}

CXQChatRosterGroup* XQChat::CXQChatRosterScene::addRosterGroupIfDontExist( const QString& group )
{
	if(!m_mapRosterGroup.contains(group))
	{
		CXQChatRosterGroup* pGroup = new CXQChatRosterGroup(group);
		pGroup->setPos(((m_mapRosterGroup.count()%4)*250+25),((m_mapRosterGroup.count()/4)*250)+25);
		m_mapRosterGroup[group] = pGroup;
		addItem(pGroup);
		return pGroup;
	}

	return 0;
}

void XQChat::CXQChatRosterScene::updateRoster( CXQChatRoster* pRoster )
{
	if(pRoster)
	{
		m_pGroup->addRosterNodeIfDontExist(pRoster);
		foreach( const QString& group, pRoster->getGroups())
		{
			if(m_mapRosterGroup.contains(group))
			{
				m_mapRosterGroup[group]->addRosterNodeIfDontExist(pRoster);
			}
			else
			{
				CXQChatRosterGroup* pGroup = addRosterGroupIfDontExist(group);
				pGroup->addRosterNodeIfDontExist(pRoster);
			}
		}
	}
}
